package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"week2/user_srv/addconfig"
)

var (
	DB  *gorm.DB
	err error
)

func InitMysql() {
	addr := addconfig.AppData.MySQLConfig
	//动态配置
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", addr.Name,
		addr.Pwd, addr.Head, addr.Pore, addr.DBName)
	//dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})

	if err != nil {
		fmt.Println("数据库链接失败")
		return
	}
	fmt.Println("数据库链接成功")

	//自动迁移
	DB.AutoMigrate(&UserWeek{})

}
